//////////////////////////////////////////////////////////////////////////////////////
//
//  类描述：
//
//        懒汉单例-BASE——MANEGE经理类
//  
//  功能描述：
//       
//        实现继承基类的所有成员 自动调用碰撞，返回真假或回调函数
//
//  功能实现：
// 
//        凡继承基类，自动添加进child
//  
//
//////////////////////////////////////////////////////////////////////////////////////
enum CBASEEVENT
{
   judge=0,
   bejudge=1,
   animeend=2
}
class CBASE_MANAGE{
    //全局成员
    private child:any[]=[]                   //子类数组
    private childmap:{[key:string]:any}={}  //子类键值对
    private MonoBehaviour:MonoBehaviour[]=[];
    public namecount:number=0;
    public colltionnamecount:number=0;
    //单例
    private static myself:CBASE_MANAGE
	private constructor() {
      
	}
    
    public static get getCBASE_MANAGE():CBASE_MANAGE
    {
        if(this.myself==undefined)this.myself=new CBASE_MANAGE
        return this.myself;
      
    }
    public pushinMonoBehaviours(ptr:MonoBehaviour)
    {
       this.MonoBehaviour.push(ptr)
    }
  
    public BASE_addchild(a:any,name?:string) //添加子类到经理类
    {

            for(var b=0;b<this.child.length;b++) //判断无名状态时是否已经添加过
            {
                if(this.child[b]==a)
                {
                    break;
                }
            }
            if(b==this.child.length) //循环到最后未被break未添加过
            {
                this.child.push(a)
            }
            if(name!=undefined&&this.childmap[name]==undefined)
            {
                this.childmap[name]=a;
            }
           
       
        
    }
    public BASE_removechild(a:any,name?:string) //从经理类中删除子类
    {
          
        for(var v=0;v<this.child.length;v++)
        {
            if(this.child[v]==a)
            {
                this.child.splice(v,1);
                   if(name!=undefined)
                     if(this.childmap[name]!=undefined)
                          this.childmap[name]=undefined
            }
        }
    }
    public BASE_removechildbyname(name:string) //使用名字为索引从经理类中删除子类
    {
      if(this.childmap[name]!=undefined)
         this.childmap[name]=undefined
        for(var v=0;v<this.child.length;v++)
        {
             if(this.child[v].objName==name)
             {
                 this.child.splice(v,1);
                 v--;
             }
        }
   
    }
     public getanyoneByname(name:string):any///使用名字为索引从经理类中找到已命名成员
    {
         return this.childmap[name]
    }
    public getALLobject():any[]
    {
        return this.child
    }
    public update()
    {
        for(let a=0;a<this.child.length;a++)
        {
            this.child[a].update();
        }
        for(let a=0;a<this.MonoBehaviour.length;a++)
        {
            this.MonoBehaviour[a].updata();
        }
    }
    public clearALL()
    {
        this.child=[]
        this.childmap={}
        this.MonoBehaviour=[]
    }
    public clearALLscript()
    {
        this.MonoBehaviour=[];
    }
    public getcollitionByshapename(name:string):CCOLLISION
    {
         for(let a=0;a<this.child.length;a++)
         {
           
           for(let b=0;b<this.child[a].judge.length;b++)
		   {
			if(this.child[a].judge[b].shape.name==name)
			{
				return this.child[a].judge[b]
			}
		   }
		   for(let b=0;b<this.child[a].unjudge.length;b++)
		   {
			if(this.child[a].unjudge[b].shape.name==name)
			{
				return this.child[a].unjudge[b]
			}
		   }
        }
        return undefined;
    }
  
    public Allcollitiontouchenabel_forCollitionEditor()
    {
         for(let a=0;a<this.child.length;a++)
         {
           
           for(let b=0;b<this.child[a].judge.length;b++)
		   {
				this.child[a].judge[b].shape.touchEnabled=true
		   }
		   for(let b=0;b<this.child[a].unjudge.length;b++)
		   {
		      this.child[a].unjudge[b].shape.touchEnabled=true
             
		   }
        }
    }
    public Allcollitiontouchenabel_forOvjectEditor()
    {

         for(let a=0;a<this.child.length;a++)
         {
           
           for(let b=0;b<this.child[a].judge.length;b++)
		   {
				this.child[a].judge[b].shape.touchEnabled=false
		   }
		   for(let b=0;b<this.child[a].unjudge.length;b++)
		   {
		      this.child[a].unjudge[b].shape.touchEnabled=false;
		   }
        }
    }
    public getobjectByjson(framename:string):boolean
    {
        //提取数据包
         let data=egret.localStorage.getItem(framename);
         if(data==null)return false;
		 let datapackge=JSON.parse(data);
         //提出obj数据
         let datas=datapackge["objdata"]
         //提出刚体数据和脚本数据
         let bodydata=datapackge["bodydata"]
         let scriptdata=datapackge["scriptdata"]
         let plattedata=datapackge["plattedata"]

         //设置编辑器正在编辑的工程名
         CMainEditor.editor.OBJECTNAME=framename;
         //先读取所有对象信息并创建
        for(let a=0;a<datas.length;a++)
        {
             let name=datas[a].name
             let resname=datas[a].resname
             var obj=new COBJECT(name)
             if(resname.split("/")[1]=="json")
             {
                 obj.addMovieClickByname(resname.split("/")[0]);
             }
             else 
             {
                 let textrue: egret.Texture= RES.getRes(resname);
                 obj.addbitmapBytextrue(textrue);
             }
             this.setobjectbasicByjson(obj,datas[a])
             this.setobjjudgeByjson(obj,datas[a].judge,true);
             this.setobjjudgeByjson(obj,datas[a].unjudge,false);
             this.namecount++;
        }
         
         //供给记录刚体和脚本的助手读取信息
         ASSIST.getASSIST.loadingByjson(bodydata,scriptdata);
         //重新创建画布
         CMainEditor.editor.loadingplatte(plattedata);
         //重新加载成员
         CMainEditor.editor.loadingALL();
         //重新布置层级
        for(let a=0;a<datas.length;a++)
        {
            let name=datas[a].name
            let index=datas[a].index
            if(ASSIST.getASSIST.platte==undefined)
              CMainEditor.editor.setplattetoASSIST();
            ASSIST.getASSIST.platte.setChildIndex(this.getanyoneByname(name),index);
        }
         
        return true;
    }

    public setobjectbasicByjson(obj:COBJECT,datas:any)
    {
         obj.objname=datas.name
		 obj.x=datas.x
		 obj.y=datas.y
		 obj.rotation= datas.rotation
		 obj.scaleX=datas.scaleX
		 obj.scaleY=datas.scaleY
		 obj.anchorOffsetX=datas.anchorOffsetX
		 obj.anchorOffsetY=datas.anchorOffsetY
		 obj.resname=datas.resname
         obj.isbackground=datas.isbackground
        
    }
    public setobjjudgeByjson(obj:COBJECT,datas:any,juorno:boolean)
    {
      for(let a=0;a<datas.length;a++)
      {
        let collitionname=datas[a].name
        let collitiontype=datas[a].judgetype  
        if(collitiontype==judgetype.circle)
        {
            obj.addbox_circlejudge(juorno,datas[a].x0,datas[a].y0,datas[a].r)
        }
        else if(collitiontype==judgetype.rectangle)
        {
            obj.addbox_rectjudge(juorno,datas[a].x0,datas[a].y0,datas[a].width,datas[a].height)
        }
        else if(collitiontype==judgetype.poly)
        {
            let pointnumber=datas[a].pointnumber
            let point:egret.Point[]=[]
            for(let b=0;b<pointnumber;b++)
            {
               point.push(new egret.Point(datas[a]["x"+b],datas[a]["y"+b]));
            }
           if(point.length==3)
           {
               obj.addbox_polyjudge(juorno,point[0],point[1],point[2]);
           }
           else if(point.length==4)
           {
               obj.addbox_polyjudge(juorno,point[0],point[1],point[2],point[3]);
           }
           else if(point.length==5)
           {
               obj.addbox_polyjudge(juorno,point[0],point[1],point[2],point[3],point[4]);
           }
        }
       
        if(juorno)
        {
            obj.judge[obj.judge.length-1].shape.name=datas[a].shapename;
            obj.judge[obj.judge.length-1].name=collitionname;
        }
        else 
        {
            obj.unjudge[obj.unjudge.length-1].shape.name=datas[a].shapename;
             obj.unjudge[obj.unjudge.length-1].name=collitionname;
        }
        this.colltionnamecount++;
      }
    }
    public Onstart()
    {
      
        for(let a=0;a<this.MonoBehaviour.length;a++)
        {
            this.MonoBehaviour[a].onstart();
        }
      
    }
    public Onend()
    {
        for(let a=0;a<this.MonoBehaviour.length;a++)
        {
            this.MonoBehaviour[a].onend();
        }
    }
    
    public getobjCloneBynamea(name:string)
    {
      
        let target=this.getanyoneByname(name)
        let result=new COBJECT(name+"_"+this.namecount++);
         if(target.resname.split("/")[1]=="json")
        {
            result.addMovieClickByname(target.resname.split("/")[0]);
        }
         else 
        {
            let textrue: egret.Texture= RES.getRes(target.resname);
             result.addbitmapBytextrue(textrue);
        }
         for(let a=0;a<target.judge.length;a++)
         {
           let collitionname=target.judge[a].name
           let collitiontype=target.judge[a].judgetype  
           if(collitiontype==judgetype.circle)
           {
            result.addbox_circlejudge(true,target.judge[a].shapepoint[0].x,target.judge[a].shapepoint[0].y,target.judge[a].r)
           }
           else if(collitiontype==judgetype.rectangle)
           {
            let width=(target.judge[a].shapepoint[1].x-target.judge[a].shapepoint[0].x)
            let height=(target.judge[a].shapepoint[2].y-target.judge[a].shapepoint[1].y)
            result.addbox_rectjudge(true,target.judge[a].shape.x,target.judge[a].shape.y,width,height)
           }
           else if(collitiontype==judgetype.poly)
           {
          
           if(target.judge[a].shapepoint.length==3)
           {
               result.addbox_polyjudge(true,target.judge[a].shapepoint[0],target.judge[a].shapepoint[1],target.judge[a].shapepoint[2]);
           }
           else if(target.judge[a].shapepoint.length==4)
           {
              result.addbox_polyjudge(true,target.judge[a].shapepoint[0],target.judge[a].shapepoint[1],target.judge[a].shapepoint[2],target.judge[a].shapepoint[3]);
           }
           else if(target.judge[a].shapepoint.length==5)
           {
               result.addbox_polyjudge(true,target.judge[a].shapepoint[0],target.judge[a].shapepoint[1],target.judge[a].shapepoint[2],target.judge[a].shapepoint[3],target.judge[a].shapepoint[4]);
           }
            result.judge[result.judge.length-1].name=collitionname;
           }
         }
         for(let a=0;a<target.unjudge.length;a++)
         {
           let collitionname=target.unjudge[a].name
           let collitiontype=target.unjudge[a].judgetype  
           if(collitiontype==judgetype.circle)
           {
            result.addbox_circlejudge(false,target.unjudge[a].shapepoint[0].x,target.unjudge[a].shapepoint[0].y,target.unjudge[a].r)
           }
           else if(collitiontype==judgetype.rectangle)
           {
            let width=(target.unjudge[a].shapepoint[1].x-target.unjudge[a].shapepoint[0].x)
            let height=(target.unjudge[a].shapepoint[2].y-target.unjudge[a].shapepoint[1].y)
            result.addbox_rectjudge(false,target.unjudge[a].shape.x,target.unjudge[a].shape.y,width,height)
           }
           else if(collitiontype==judgetype.poly)
           {
          
           if(target.unjudge[a].shapepoint.length==3)
           {
               result.addbox_polyjudge(false,target.unjudge[a].shapepoint[0],target.unjudge[a].shapepoint[1],target.unjudge[a].shapepoint[2]);
           }
           else if(target.unjudge[a].shapepoint.length==4)
           {
              result.addbox_polyjudge(false,target.unjudge[a].shapepoint[0],target.unjudge[a].shapepoint[1],target.unjudge[a].shapepoint[2],target.unjudge[a].shapepoint[3]);
           }
           else if(target.unjudge[a].shapepoint.length==5)
           {
               result.addbox_polyjudge(false,target.unjudge[a].shapepoint[0],target.unjudge[a].shapepoint[1],target.unjudge[a].shapepoint[2],target.unjudge[a].shapepoint[3],target.unjudge[a].shapepoint[4]);
           }
            result.unjudge[result.judge.length-1].name=collitionname;
         }
         }
         result.x=target.x
		 result.y=target.y
		 result.rotation= target.rotation
		 result.scaleX=target.scaleX
		 result.scaleY=target.scaleY
		 result.anchorOffsetX=target.anchorOffsetX
		 result.anchorOffsetY=target.anchorOffsetY
		 result.resname=target.resname
         result.isbackground=target.isbackground
         result.name=target.objName
      
      return result;


    }
    public ALLobjCollition()
    {
        for(let a=0;a<this.child.length;a++)
        {
            if(this.child[a].judge.length)
            {
               COLLISION_MANAGE.getCOLLISIONCONTRL.ADDjudge(this.child[a])
            }
        }
    }

}